/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import com.eelpo.common.util.BlankUtils;
import com.eelpo.common.util.StringUtils;

/**
 * <pre>
 * 
 * order表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class OrderExpression implements CompoundCriterion {

	private boolean useAlias;
	private String mode;
	private String property;

	private OrderExpression(boolean useAlias, String mode, String property) {
		this.useAlias = useAlias;
		this.mode = mode;
		this.property = property;
	}

	public static OrderExpression getOrderExpression(String order) {
		if (BlankUtils.isBlank(order)) {
			return null;
		}
		return new OrderExpression(true, StringUtils.EMPTY, order);
	}

	public static OrderExpression getOrderInornateExpression(String order) {
		if (BlankUtils.isBlank(order)) {
			return null;
		}
		return new OrderExpression(false, StringUtils.EMPTY, order);
	}

	public static OrderExpression getOrderExpression(String property, String mode) {
		return new OrderExpression(true, " ".concat(mode), property);
	}

	public static OrderExpression getOrderInornateExpression(String property, String mode) {
		return new OrderExpression(false, " ".concat(mode), property);
	}

	public static OrderExpression getAscOrderExpression(String property) {
		return new OrderExpression(true, ASC, property);
	}

	public static OrderExpression getAscOrderInornateExpression(String property) {
		return new OrderExpression(false, ASC, property);
	}

	public static OrderExpression getDescOrderExpression(String property) {
		return new OrderExpression(true, DESC, property);
	}

	public static OrderExpression getDescOrderInornateExpression(String property) {
		return new OrderExpression(false, DESC, property);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.CompoundCriterion#getJpql(java.lang.StringBuilder
	 *      )
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		if (useAlias) {
			jpql.append(alias);
		}
		jpql.append(property);
		jpql.append(mode);
		return jpql;
	}

}
